package com.qf.springboot2.controller;

import com.qf.springboot2.pojo.Stu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/stu")

//启用指定类型的实体，spring将这个实体创建实例对象，并将application中对应属性封装到这个实例对象的属性上
//同时将封装数据后的实例对象添加到spring容器中
@EnableConfigurationProperties(Stu.class)
public class StudentController {

    @Value("${stu.name}")
    private String name;

    @Value("${stu.age}")
    private Integer age;

    @Value("${stu.friends[2]}")
    private String friend;

    @Autowired
    private Environment env;

    @Autowired
    private Stu stu;

    /**
     * 只能拿单值
     * @return
     */
    @GetMapping("/test1")
    public String test1(){
        return name + "-" + age + "-" + friend;
    }

    /**
     * 只能拿单值
     * @return
     */
    @GetMapping("/test2")
    public String test2(){

        String name = env.getProperty("stu.name");
        Integer age = env.getProperty("stu.age", Integer.class);
        String friend = env.getProperty("stu.friends[0]");

        return name + "-" + age + "-" + friend;
    }

    @GetMapping("/test3")
    public Stu test3(){
        return stu;
    }
}
